package com.demo0_求最大的单词长度;

public class MaxWordsLength {
    public static void main(String[] args) {
        int max = getMax(" a  ");
        System.out.println(max);


    }

    public static int getMax(String words) {
        //1. 先去掉前后空格
        if (words == null || words.length() == 0) {
            return 0;
        }
        String trim = words.trim();
        int length = trim.length();
        int flag = 0;
        int max = 0;
        int temp = 0;

        for (int i = 0; i < length; i++) {
            if (trim.charAt(i) == ' ') {
                temp = i - flag;
                max=(temp>max?temp:max);
                flag = i + 1;

            }
            if (i == (length - 1)) {
                int last = length-flag;
                max=(last>max?last:max);
            }
        }
        return max;
    }
}
